<template>
	<view class="u-page">
		<view class="top-ad" v-if="globalConfig && globalConfig.topAd && globalConfig.topAd.length">
			<swiper class="swiper" :indicator-dots="globalConfig.topAd.length > 1" autoplay>
				<swiper-item v-for="(item, index) in globalConfig.topAd" :key="index">

						<image :src="getFmtImage(item.image)" mode="scaleToFill" @click="handleMenuClick(item)">
							<button open-type="contact" v-if="item.contact"
								style="position: fixed;width: 100%;height: 100%;top: 0;z-index: 1;opacity: 0;"
								show-message-card               
								:send-message-img="getFmtImage(item.contact.image)"
								:send-message-title="item.contact.title"
								:send-message-path="item.contact.path || 'pages/index/index'">
							</button>
						</image>
				</swiper-item>
			</swiper>
		</view>
        
        <view v-if="noticeText && noticeText.length" style="margin: 10rpx 30rpx 0;">
        	<u-notice-bar :text="noticeText" 
                bgColor="#ff8d00"
                color="#fff"
                direction="column" @click="handleNoticeTextClick"></u-notice-bar>
        </view>
        
		<swiper class="swiper" :class="{shortMobile: shortMobile}" :indicator-dots="menus.length > 1" v-if="menus">
			<swiper-item v-for="(group, mid) in menus" :key="mid">
				<view class="items">
					<view class="item" v-for="(item, index) in group" :key="index" @click="handleMenuClick(item)" v-show="!isRelease || !item.hide">
						<image :src="item.image" mode="aspectFill"></image>
					</view>
				</view>
			</swiper-item>
		</swiper>
		<view v-if="globalConfig">
			<registerLogin ref="loginPopup" @onlogin="onUserLogin"></registerLogin>
		</view>
        <view class="version" @click="updateMp()">V:{{version}}</view>
	</view>
</template>

<script>
	import _ from 'lodash';
	import getLocation from '@/utils/getLocation.js'
	import {mapState, mapMutations} from 'vuex';
	import sceneMixins from '@/utils/sceneMixins.js';
	import registerLogin from '@/components/register-login/registerLogin.vue'
	export default {
		components: {registerLogin},
		data() {
			return {
				version: '2.16.5',
				menus: '',
				shortMobile: uni.getSystemInfoSync().windowHeight < 750,
                // #ifdef MP-WEIXIN
				isRelease: __wxConfig.envVersion === 'release',
                // #endif
                // #ifdef H5
                isRelease: location.hostname != 'localhost',
                // #endif
                noticeText: '',
			}
		},
		computed: {
			...mapState(['globalConfig'])
		},
		mixins:[ getLocation, sceneMixins],
		onLoad (options) {
			var vm = this;

			var scene = decodeURIComponent(options.scene);
			if (scene != 'undefined') {
				var sceneId = scene.split('$')[0];
				var value = scene.split('$')[1];
				uni.showLoading({
					duration: 1500,
				})
				setTimeout(() => {
					vm.parseScene(sceneId, value, '扫码');
				}, 1500);
			}
			wx.showShareMenu({
				withShareTicket: true,
				menus: ["shareAppMessage","shareTimeline"]
			});
			// 因为隐私政策先关闭下列代码
			// setTimeout(() => {
			// 	this.getUserLocation().then(res => {
			// 		this.setPosition(res);
			// 	})
			// }, 5000);
		},
		mounted: function () {
			var vm = this;
			uni.showLoading();
            vm.waitVariable('globalConfig').then(() => {
				vm.doWxLogin().then(res => {
					vm.openId = res.openId;

					vm.getAndLoad('首页菜单', vm.globalConfig.indexMenuConfig, function (res) {
						// #ifdef H5
						res.forEach(n => {
							if (n.miniApp) {
								n.hide = true;
							}
						});
						// #endif
						res = res.filter(n => !n.hide);
						var groupMenus = _.chunk(res, vm.shortMobile ? 6 : 8);
						vm.menus = groupMenus;
						uni.hideLoading();
					});
				});
                vm.globalConfig.indexPageNoticeTextUrl && vm.getAndLoad('首页通知', vm.globalConfig.indexPageNoticeTextUrl, (res) => {
                    vm.noticeTextArr = res.data;
                    vm.noticeText = res.data.map(n => n.title);
                })
            })
            

            
		},
		options: { styleIsolation: 'shared' },
		methods: {
			...mapMutations(['setPosition']),
			handleMenuClick: function (item, donotTip) {
				var vm = this;
				if (item.needLogin && !vm.hasLogin) {
					this.$refs.loginPopup.openCommonPopup();
					return;
				}
				// if (item.name == '个人惠泽码') {
				// if (item.image.search('23.png')!=-1) {
				// 		vm.gotoH5Page('https://lyggn.gnxzhzx.com/mobile/h5/huizetong/grhzm/grhzmNocache.html?needRefresh=true&id=' + vm.openId);
				// 	return;
				// }
				if (item.url) {
					uni.navigateTo({
						url: item.url
					})
				} else if (item.miniApp) {
					uni.navigateToMiniProgram({
						appId: item.miniApp,
						path: item.path
					});
				} else if (item.event) {
					this.doEvent(item.event);
				} else if (item.h5) {
					this.gotoH5Page(item.h5);
				} else if (!donotTip){
					uni.$u.toast('功能正在开发中，请明天再来');
				}
			},
			doEvent(event) {
				if (event == 'scan') {
					 uni.scanCode({
                        onlyFromCamera: true, //只能从相机中获取，不能从图片中获取
                        success: function(res) { //获取扫码结果
							uni.$u.toast(res);
                        }, fail: function (res) {
							uni.$u.toast('扫码失败');
						}
                    });
				}
			},
			updateMp: function () {
				var vm = this;
				uni.showLoading();
				if (wx.canIUse('getUpdateManager')) { //判断当前微信版本是否支持版本更新
					const updateManager = wx.getUpdateManager();
			
					updateManager.onCheckForUpdate(function(res) {
			
						if (res.hasUpdate) { // 请求完新版本信息的回调
			
							updateManager.onUpdateReady(function() {
								uni.hideLoading();
								uni.$u.toast('有新版本可以更新，即将更新');
								setTimeout(() => {
									updateManager.applyUpdate();
								}, 100)
							});
			
							updateManager.onUpdateFailed(function() {
								uni.hideLoading();
								wx.showModal({ // 新的版本下载失败
			
									title: '已经有新版本了哟~',
			
									content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
			
								})
			
							})
			
						} else {
							uni.hideLoading();
							uni.navigateTo({url: '/pages/shenbao/list?loadJSON=/mobile/wxJSON/更新日志/config.json'})
						}
					})
			
				}
			},
            handleNoticeTextClick: function (index) {
                this.handleMenuClick(this.noticeTextArr[index], 1);
            }
		}
	}
</script>

<style lang="scss" scoped>
	.u-page {
		background: url('#{$APIPATH}/mobile/wxImages/hzt/banner.png'), url('#{$APIPATH}/mobile/wxImages/hzt/bg2.png') bottom;
		background-color: #f6f7f6;
		height: 100vh;
        /* #ifdef H5 */
        height: calc(100vh - 44px);
        /* #endif */
        
		background-size: 100%;
		background-repeat: no-repeat;
		overflow: auto;
		padding-top: 280rpx;
		box-sizing: border-box;
		.swiper {
			height: 905rpx;
			margin-top: 10rpx;
			&.shortMobile {
				height: 700rpx;
			}
			.items {
				display: flex;
				flex-wrap: wrap;
				.item {
					width: 50%;
					image {
						width: 371rpx;
						height: 206rpx;
					}
				}
			}
		}
	}
	.version {
        margin-top: 10rpx;
        text-align: right;
        margin-right: 20rpx;
        font-size: 26rpx;
        display: inline-block;
        float: right;
	}

	.top-ad {
		height: 162rpx;
		// position: absolute;
		z-index: 1;
		top: 260rpx;
		width: 700rpx;
		margin-left: 25rpx;
		/deep/ .u-swiper {
			height: 162rpx !important;
		}
		.swiper {
			height: 162rpx;
			margin-top: 0;
			image {
				width: 100%;
				height: 100%;
			}
		}
	}
</style>
